﻿using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XYX.Print.Impl;
using XYX.Print.Models;

namespace XYX.Print.Client.PrintUI
{
    /// <summary>
    /// 条码查询
    /// </summary>
    public partial class ScanBarCodeFormList : UIForm
    {
        /// <summary>
        /// 构造
        /// </summary>
        public ScanBarCodeFormList()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
         
            BindGrid();
        }
        /// <summary>
        /// 加载 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScanBarCodeFormList_Load(object sender, EventArgs e)
        {
            this.dgvBarCode.RowHeadersVisible= true;
            this.dgvBarCode.AutoGenerateColumns = false;
            this.dgvBarCode.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            txtStartDate.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:01"));
            txtEndDate.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            dgvBarCode.RowsAdded += DgvBarCode_RowsAdded;
            BindGrid();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DgvBarCode_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            for (int i = 0; i < e.RowCount; i++)
            {
                dgvBarCode.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvBarCode.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
            }

        }





        /// <summary>
        /// 绑定表格
        /// </summary>
        /// <param name="barCode"></param>
        /// <param name="startDateTime"></param>
        /// <param name="endDateTime"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        private void BindGrid(int pageIndex = 1)
        {
         
            string barCode = this.txtBarCode.Text;
            string groupCode = txtGroupCode.Text;
            int count = 0;
            int pageSize = this.uiPagination.PageSize;
            ScanBarCodeRecordImpl scanBarCodeRecordImpl = new ScanBarCodeRecordImpl();
            List<ScanBarCodeRecord> scanBarCodeRecord = scanBarCodeRecordImpl.GetListPaged(pageIndex, pageSize, barCode, groupCode, txtStartDate.Value, txtEndDate.Value, out count).ToList();

            var dt = scanBarCodeRecord.ToDataTable();
            this.dgvBarCode.DataSource = dt;
            this.uiPagination.TotalCount = count;
            this.uiPagination.PageSize = 20;

        }
        /// <summary>
        /// 页切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pagingSource"></param>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        private void uiPagination_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            BindGrid(pageIndex);
        }
    }
}
